package com.easyExcel.controller;

import com.alibaba.excel.EasyExcel;
import com.easyExcel.dto.User;
import com.easyExcel.dto.Worker;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/excel")
public class WriteExcel {

    // 根据user模板构建数据
    private List<User> getUserData() {
        List<User> users = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            User user = User.builder()
                    .userId(i)
                    .userName("admin" + i)
                    .gender(i % 2 == 0 ? "男" : "女")
                    .salary(i * 1000.00)
                    .hireDate(new Date())
                    .build();
            users.add(user);
        }
        return users;
    }

    @Test
    public  void testWriteExcel() {

        String filename = "D:\\test\\Excel\\user1.xlsx";

        EasyExcel.write(filename,User.class)
                .sheet("用户信息")
                .doWrite(getUserData());
    }

    // TODO: 2023/3/15 复杂头
    @Test
    public void testWriteExcel6() {
        String filename = "D:\\test\\Excel\\user2.xlsx";
        List<Worker> users = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            Worker worker = Worker.builder()
                    .userId(i)
                    .userName("工人" + i)
                    .hireDate(new Date())
                    .build();
            users.add(worker);
        }
        // 向Excel中写入数据
        EasyExcel.write(filename, Worker.class)
                .sheet("用户信息")
                .doWrite(users);
    }


}